package com.gitee.feizns.ding.talk.zzd;

import com.alibaba.xxpt.gateway.shared.client.http.api.OapiSameResponse;
import com.alibaba.xxpt.gateway.shared.client.http.api.OapiSpResultContent;
import com.alibaba.xxpt.gateway.shared.client.http.api.OapiSpResultResponse;
import com.gitee.feizns.quickstart.jackson.JsonUtils;
import com.gitee.feizns.quickstart.jackson.Val;

/**
 * 工具类
 * @author feizns
 * @since 2025/04/06
 */
public class Utils {

    /**
     * if 失败抛出
     * @param resp {@link OapiSpResultResponse}
     * @return {@link OapiSpResultContent }
     */
    public static OapiSpResultContent getContentIfFailThrow(OapiSpResultResponse resp) {
        if ( !resp.getSuccess() || !resp.getContent().getSuccess() )
            throw new RuntimeException(JsonUtils.toJson(resp));
        return resp.getContent();
    }

    /**
     * if 失败抛出
     * @param resp {@link OapiSpResultResponse}
     * @return {@link Val }
     */
    public static Val getContentIfFailThrow(OapiSameResponse resp) {
        if ( Boolean.FALSE.equals(resp.getSuccess()) )
            throw new RuntimeException(JsonUtils.toJson(resp));
        Val content = Val.of(resp.getContent());
        if ( !content.get("success").booleanValue() )
            throw new RuntimeException(JsonUtils.toJson(resp));
        return content;
    }

}
